﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SocialShare.BLL;
using System.Configuration;
using System.Web.Security;
using SocialShare.BusinessEntities;
using SocialShare.BusinessLogicLayer;
using Utilities;
using SocialShare.DAL.Facades;


namespace SocialShare.Web.Integrations
{
    public class StrategyProxyUser : IUserBLL
    {
        dynamic _userBLL;
        public Boolean IsMembershipUsed
        {
            get;
            set;
        }

        public String User_Name
        {
            get;
            set;
        }

        public String User_ID
        {
            get;
            set;
        }

        public StrategyProxyUser()
        {
            ConfigReader configReader = new ConfigReader();
            String configValue = configReader.ReadConfigurationValue(ConfigurationManager.AppSettings[APP_SETTINGS_NAME.SiteConfigLocation.ToString()], SITE_CONFIG.MEMBERSHIP_PROVIDER_USED.ToString());
            if (configValue != "YES")
            {
                this.IsMembershipUsed = false;
            }
            else
            {
                this.IsMembershipUsed = true;
            }

        }

        public StrategyProxyUser GetCurrentUser()
        {
            if (IsMembershipUsed)
            {
               MembershipUser currentLoggedInUser = Membership.GetUser();
               this.User_Name = currentLoggedInUser.UserName;
               this.User_ID = currentLoggedInUser.ProviderUserKey.ToString();
               CookieManager.LOGGED_IN_USER_ID = this.User_ID;
               return this;
            }
            else
            {
                ///Custom user model
                if (!String.IsNullOrEmpty(SessionManager.LOGGED_IN_USER_ID))
                {
                    MemberFacade memFacade = new MemberFacade();
                    SocialShare_Members memUser = memFacade.GetMemberByMember_Id(Convert.ToInt32(SessionManager.LOGGED_IN_USER_ID));
                    this.User_Name = memUser.ScreenName;
                    this.User_ID = memUser.Member_Id.ToString();
                    return this;
                }
                return null;
            }
        }

        public void Dispose()
        {
            _userBLL.Dispose();
        }

        public List<StrategyProxyUser> GetAllUsers()
        {
            if (IsMembershipUsed)
            {
                return ConvertToUser(Membership.GetAllUsers());
            }
            else
            {
                ///Custom user model
                MemberFacade memFacade = new MemberFacade();
                return ConvertToUser(memFacade.GetAllMembers());
            }
        }

        public String GetUserName(String userId = default(String))
        {
            if (IsMembershipUsed)
            {
                //Guid UserID = new Guid(userId);
                MembershipUser memUser = Membership.GetUser(userId);
                return memUser.UserName;
            }
            else
            {
                ///Custom user model
                MemberFacade memFacade = new MemberFacade();
                SocialShare_Members memUser = memFacade.GetMemberByMember_Id(Convert.ToInt32(userId));
                return memUser.ScreenName;
            }
        }

        private List<StrategyProxyUser> ConvertToUser(MembershipUserCollection memUsers)
        {
            List<StrategyProxyUser> users = new List<StrategyProxyUser>();

            foreach (MembershipUser memUser in memUsers)
            {
                StrategyProxyUser user = new StrategyProxyUser();
                user.User_Name = memUser.UserName;
                user.User_ID = memUser.ProviderUserKey.ToString();
                users.Add(user);
            }

            return users;
        }

        private List<StrategyProxyUser> ConvertToUser(List<SocialShare_Members> memUsers)
        {
            List<StrategyProxyUser> users = new List<StrategyProxyUser>();
            ///Custom user model
            foreach (SocialShare_Members memUser in memUsers)
            {
                StrategyProxyUser user = new StrategyProxyUser();
                user.User_Name = memUser.ScreenName;
                user.User_ID = memUser.Member_Id.ToString();
                users.Add(user);
            }

            return users;
        }
    }
}